package com.app.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.app.model.PropertyManagementCompany;
import com.app.service.IPropertyManagementCompanyService;
import com.app.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 物业公司配置 前端控制器
 * </p>
 *
 * @author yoominic
 * @since 2025-02-27
 */
@Api(tags = "物业公司配置")
@RestController
@RequestMapping("/propertyManagementCompany")
public class PropertyManagementCompanyController {

    @Autowired
    private IPropertyManagementCompanyService propertyManagementCompanyService;

    @ApiOperation(value = "获取物业公司列表", notes = "获取物业公司列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "keyword", value = "搜索关键字", required = false, paramType = "query", dataType = "string")
    })
    @GetMapping("/adminGetCompanyList")
    public Result adminGetCompanyList(Integer page, Integer limit, String keyword) {

        Page<PropertyManagementCompany> pageInfo = new Page<>(page, limit);
        QueryWrapper<PropertyManagementCompany> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        if (keyword != null && !keyword.equals("")) {
            queryWrapper.like("company_name", keyword).or().like("company_phone", keyword);
        }
        queryWrapper.orderByDesc("create_time");
        Page<PropertyManagementCompany> pageList = propertyManagementCompanyService.page(pageInfo, queryWrapper);

        JSONObject result = new JSONObject();
        result.put("total", pageList.getTotal());
        result.put("list", pageList.getRecords());
        return Result.success(result);
    }

    @ApiOperation(value = "保存物业公司信息", notes = "保存物业公司信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "propertyManagementCompany", value = "物业公司信息", required = true, paramType = "body", dataType = "PropertyManagementCompany")
    })
    @PostMapping("/adminSaveCompany")
    public Result adminSaveCompany(@RequestBody PropertyManagementCompany propertyManagementCompany) {
        Integer loginUserId = StpUtil.getLoginIdAsInt();
        if (propertyManagementCompany.getId() == null) {
            propertyManagementCompany.setCreateId(loginUserId);
            propertyManagementCompany.setCreateTime(LocalDateTime.now());
            propertyManagementCompanyService.save(propertyManagementCompany);
        }else {
            propertyManagementCompany.setUpdateId(loginUserId);
            propertyManagementCompany.setUpdateTime(LocalDateTime.now());
            propertyManagementCompanyService.updateById(propertyManagementCompany);
        }
        return Result.success("操作成功！");
    }

}

